package com.itheima.job;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SetJob {
    @Autowired
    private OrderSettingDao orderSettingDao;


    public void setNumber() {
        Date firstDate = DateUtils.nextMonthFirstDate();
        Date lastDate = DateUtils.nextMonthLastDate();
        List<OrderSetting> list = new ArrayList<OrderSetting>();
        for (Date d = firstDate; d.compareTo(lastDate) <= 0; d = DateUtils.getNextDayMonday(d)) {
            try {
                list.add(new OrderSetting(d, 300));
                System.out.println(DateUtils.parseDate2String(d));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                Integer count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    orderSettingDao.update(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

}
